package com.example.majiang.controller;

import com.example.majiang.dto.PaginationDTO;
import com.example.majiang.dto.QuesitionDTO;
import com.example.majiang.mapper.UserMappper;
import com.example.majiang.model.User;
import com.example.majiang.service.QuestionService;
import com.example.majiang.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class indexController {
    /*@Autowired
    private UserMappper userMappper;*/
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private QuestionService questionService;


    @GetMapping("/")
    public String index(HttpServletRequest request, Model model, @RequestParam(name = "page", defaultValue = "1") Integer page,
                        @RequestParam(name = "size", defaultValue = "5") Integer size){
        Cookie[]cookies=request.getCookies();
        /*for(Cookie cookie:cookies){
            if(cookie.getName().equals("token")){
                String token=cookie.getValue();
                User user=userMappper.findByToken(token);
                if(user!=null){
                    request.getSession().setAttribute("user",user);
                }
                break;
            }
        }*/
        User user=(User)request.getSession().getAttribute("user");
        if(user!=null){
            request.getSession().setAttribute("user",user);
        }
        PaginationDTO pagination = questionService.list(page, size);
        model.addAttribute("pagination", pagination);

        int i=0;
        return "index";

    }
}
